from starlette.endpoints import HTTPEndpoint
from starlette.responses import JSONResponse
from model.ModEmpWorkloadRep import EmpWorkloadRep, empWorkloadRepSchema
from database import Session, engine
import datetime


def get_emp_workload_rep(session, begin_time: datetime, end_time: datetime, openid: str, bureauid: str):
    # 人员工作量统计表
    connection = engine.raw_connection()
    try:
        cursor = connection.cursor()
        cursor.callproc("statics_emp_work", (begin_time, end_time, openid, bureauid))
        cursor.close()
        connection.commit()
        emp_workload = session.query(EmpWorkloadRep).filter(EmpWorkloadRep.bureauid == bureauid).filter(
            EmpWorkloadRep.openid == openid).all()
        return JSONResponse(empWorkloadRepSchema.dump(emp_workload, many=True).data)
    finally:
        connection.close()
